﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using DomainDriver.DomainModeling.DomainModels;
using DomainDriver.DomainModeling.DomainObjects;
using DomainDriver.DomainModeling.Events;
using DomainDriver.DomainModeling.Repositories;
using DomainDriver.DomainModeling.Queries;

namespace DomainDriver.DomainModeling.Constraints
{
    public delegate KEYED_DOMAIN_OBJECT SingletonCreationDelegate<KEY, KEYED_DOMAIN_OBJECT>(IDomainModel domainModel)
        where KEYED_DOMAIN_OBJECT : IKeyedDomainObject<KEY, KEYED_DOMAIN_OBJECT>;

    public class SingletonConstraint<KEY, KEYED_DOMAIN_OBJECT> : ConstraintBase
        where KEYED_DOMAIN_OBJECT : IKeyedDomainObject<KEY, KEYED_DOMAIN_OBJECT>
    {
        protected SingletonCreationDelegate<KEY, KEYED_DOMAIN_OBJECT> m_SetupDelegate;

        public SingletonConstraint(IDomainModel domainModel, SingletonCreationDelegate<KEY, KEYED_DOMAIN_OBJECT> setupDelegate)
            : base(domainModel)
        {
            if (setupDelegate == null)
            { throw new ApplicationException(); }

            m_SetupDelegate = setupDelegate;
            SingletonRepository.AddingDomainObject += new AddingDomainObjectDelegate<KEY, KEYED_DOMAIN_OBJECT>(SingletonRepository_AddingDomainObject);
            SingletonRepository.UpdatingDomainObject += new UpdatingDomainObjectDelegate<KEY, KEYED_DOMAIN_OBJECT>(SingletonRepository_UpdatingDomainObject);
            SingletonRepository.RemovingDomainObject += new RemovingDomainObjectDelegate<KEY, KEYED_DOMAIN_OBJECT>(SingletonRepository_RemovingDomainObject);
        }

        public override bool RequiresSetup
        {
            get
            {
                return true;
            }
        }

        protected IRepository<KEY, KEYED_DOMAIN_OBJECT> SingletonRepository
        {
            get { return DomainModel.GetRepository<KEY, KEYED_DOMAIN_OBJECT>(); }
        }

        protected void SingletonRepository_AddingDomainObject(IRepository<KEY, KEYED_DOMAIN_OBJECT> sender, AddDomainObjectEventArgs<KEY, KEYED_DOMAIN_OBJECT> args)
        {
            if (!Enabled)
            { return; }

            throw new ApplicationException();
        }

        protected void SingletonRepository_UpdatingDomainObject(IRepository<KEY, KEYED_DOMAIN_OBJECT> sender, UpdateDomainObjectEventArgs<KEY, KEYED_DOMAIN_OBJECT> args)
        {
            if (!Enabled)
            { return; }

            if (!args.OriginalObject.Key.Equals(args.UpdatedObject.Key))
            {
                throw new ApplicationException();
            }
        }

        protected void SingletonRepository_RemovingDomainObject(IRepository<KEY, KEYED_DOMAIN_OBJECT> sender, RemoveDomainObjectEventArgs<KEY, KEYED_DOMAIN_OBJECT> args)
        {
            if (!Enabled)
            { return; }

            throw new ApplicationException();
        }

        public override void Setup()
        {
            bool previousEnabledValue = Enabled;
            Enabled = false;

            if (SingletonRepository.Count() > 1)
            {
                ICollection<KEYED_DOMAIN_OBJECT> existingObjects = SingletonRepository.Read();
                foreach (KEYED_DOMAIN_OBJECT domainObject in existingObjects)
                {
                    SingletonRepository.Remove(domainObject.Key);
                }
            }

            if (SingletonRepository.Count() < 1)
            {
                KEYED_DOMAIN_OBJECT singleton = m_SetupDelegate(DomainModel);
                SingletonRepository.Add(singleton);
            }

            Enabled = previousEnabledValue;
        }
    }
}